﻿Imports Framework.Data

Public Class DownloadsFileSubscriptions
    Private Shared mCache As New Generic.List(Of Entities.Downloads.FileSubscription)

    Shared Sub New()
        Try
            SyncLock mCache
                For Each clsFileSub As Entities.Downloads.FileSubscription In DataStore.DownloadsFileSubscriptionsEx.GetAll
                    mCache.Add(clsFileSub)
                Next
            End SyncLock
        Catch ex As Exception
            Framework.Misc.HandleException(ex)
        End Try
    End Sub

    Public Shared Sub DeleteAllByBuddyId(ByVal buddyId As Integer, ByVal transactionScope As TransactionScope)
        SyncLock mCache
            For Each clsSub As Entities.Downloads.FileSubscription In (From s In mCache Where (s.BuddyId = buddyId) Select s).ToArray
                DataStore.DownloadsFileSubscriptionsEx.Delete(clsSub.Id, transactionScope)
                mCache.Remove(clsSub)
            Next
        End SyncLock
    End Sub
    Public Shared Sub Save(ByVal subscription As Entities.Downloads.FileSubscription, ByVal transactionScope As TransactionScope)
        SyncLock mCache
            Dim blnNew As Boolean = subscription.Id = 0
            DataStore.DownloadsFileSubscriptionsEx.Save(subscription, transactionScope)
            If blnNew Then mCache.Add(subscription)
        End SyncLock
    End Sub
    Public Shared Function GetBySHA512HashAndSize(ByVal sha512 As String, ByVal size As Long) As Generic.List(Of Entities.Downloads.FileSubscription)
        SyncLock mCache
            Return (From s In mCache Where (s.SHA512Hash = sha512 And s.Size = size) Select s).ToList
        End SyncLock
    End Function

End Class
